#!/usr/bin/env pwsh
# PowerShell 7 脚本 - 替代原来的批处理脚本

# 设置控制台编码为GB2312，因为jenkins里的环境是Windows默认的
$gb2312 = [System.Text.Encoding]::GetEncoding(936)
[Console]::InputEncoding = $gb2312
[Console]::OutputEncoding = $gb2312

# 第一个 SSH 命令：同步代码
Write-Host "正在同步代码..." -ForegroundColor Green
$sshCommand1 = @'
set -evx ;
cd openharmony/oh-xts-automation/ &&
rm -rf * &&
repo sync -c -j32 --force-sync
'@

$exitCode1 = & C:\Windows\System32\OpenSSH\ssh.exe -i C:\Users\fengziteng\.ssh\id_rsa fengziteng@ubuntu $sshCommand1

if ($LASTEXITCODE -ne 0) {
    Write-Host "警告: 代码同步可能存在问题" -ForegroundColor Yellow
}

# https://gitcode.com/openharmony/docs/blob/master/zh-cn/application-dev/faqs/full-sdk-switch-guide.md
# https://gitcode.com/openharmony/docs/tree/master/zh-cn/release-notes

# API 23
# https://cidownload.openharmony.cn/version/Daily_Version/OpenHarmony_6.1.0.20/20251201_020625/version-Daily_Version-OpenHarmony_6.1.0.20-20251201_020625-ohos-sdk-full.tar.gz

# API Version 20 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v6.0-release.md
# https://repo.huaweicloud.com/openharmony/os/6.0-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/6.0-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# API Version 18 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v5.1.0-release.md
# https://repo.huaweicloud.com/openharmony/os/5.1.0-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/5.1.0-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# API Version 14 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v5.0.2-release.md
# https://repo.huaweicloud.com/openharmony/os/5.0.2-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/5.0.2-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# API Version 13 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v5.0.1-release.md
# https://repo.huaweicloud.com/openharmony/os/5.0.1-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/5.0.1-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# API Version 12 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v5.0.0-release.md
# https://repo.huaweicloud.com/openharmony/os/5.0.0-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/5.0.0-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# API Version 11 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v4.1-release.md
# https://repo.huaweicloud.com/openharmony/os/4.1-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/4.1-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# API Version 10 Release
# https://gitcode.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v4.0-release.md
# https://repo.huaweicloud.com/openharmony/os/4.0-Release/ohos-sdk-windows_linux-public.tar.gz
# https://repo.huaweicloud.com/openharmony/os/4.0-Release/ohos-sdk-windows_linux-public.tar.gz.sha256

# 第二个 SSH 命令：下载预编译工具并设置环境
Write-Host "正在下载预编译工具并设置环境..." -ForegroundColor Green
$sshCommand2 = @'
set -vx ;
cd openharmony/ &&
rm -rf openharmony_prebuilts/ OpenHarmony_canary_prebuilts/ &&
cd oh-xts-automation/ &&
build/prebuilts_download.sh ;
rm -rf ./out ;
sed -i 's#unzip ohcommandline-tools-linux.zip#unzip ohcommandline-tools-linux.zip > /dev/null 2>\&1#' build/build_scripts/build.sh ;
mkdir -p prebuilts/ohos-sdk/linux/12 ;
for file in ../ohos-sdk-api-12-5.0.0-linux/*.zip; do
    unzip $file -d ./prebuilts/ohos-sdk/linux/12/ > /dev/null 2>&1 ;
done;
mkdir -p prebuilts/ohos-sdk/linux/14 ;
for file in ../ohos-api-14-sdk/*.zip; do
    unzip $file -d ./prebuilts/ohos-sdk/linux/14/ > /dev/null 2>&1 ;
done ;
mkdir -p prebuilts/ohos-sdk/linux/18 ;
for file in ../ohos-api-18-sdk-5.1.0-release/*.zip; do
    unzip $file -d ./prebuilts/ohos-sdk/linux/18/ > /dev/null 2>&1 ;
done ;
mkdir -p prebuilts/ohos-sdk/linux/19 ;
for file in ../ohos-sdk-master-2025-05-08/*.zip; do
    unzip $file -d ./prebuilts/ohos-sdk/linux/19/ > /dev/null 2>&1 ;
done ;
mkdir -p prebuilts/ohos-sdk/linux/20 ;
for file in ../ohos-sdk-api-20-6.0-beta1/*.zip; do
    unzip $file -d ./prebuilts/ohos-sdk/linux/20/ > /dev/null 2>&1 ;
done ;
mkdir -p prebuilts/ohos-sdk/linux/23 ;
for file in ../ohos-api-23-sdk/*.zip; do
    unzip $file -d ./prebuilts/ohos-sdk/linux/23/ > /dev/null 2>&1 ;
done
'@

$exitCode2 = & C:\Windows\System32\OpenSSH\ssh.exe -i C:\Users\fengziteng\.ssh\id_rsa fengziteng@ubuntu $sshCommand2

if ($LASTEXITCODE -ne 0) {
    Write-Host "警告: 预编译工具设置可能存在问题" -ForegroundColor Yellow
}

# 第三个 SSH 命令：编译（关键步骤）
# pip 安装 json5 是因为 build/scripts/compile_app.py 依赖了 json5 ，不安装编译 hap 会报错
Write-Host "正在编译..." -ForegroundColor Green
$sshCommand3 = @'
set -evx ;
export XTS_SUITENAME=acts ;
export target_subsystem=security ;
cd openharmony/oh-xts-automation/ ;
prebuilts/python/linux-x86/current/bin/python3 -m pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple --trusted-host repo.huaweicloud.com json5 &&
./build.sh --product-name rk3568 --target-cpu arm --gn-args build_xts=true --build-target test/xts/acts/security/huks:huks --build-target deploy_testtools --gn-args is_standard_system=true --get-warning-list=false --stat-ccache=true --compute-overlap-rate=false --deps-guard=false --gn-args skip_generate_module_list_file=true --build-target out/rk3568/build_configs/security/huks:huks_test --build-target base/security/huks:huks_components --build-target base/security/huks/test:unittest --no-prebuilt-sdk &&
rm -f ../huks_test.tgz &&
/bin/bash -c 'find out/rk3568/tests/unittest/huks/ out/rk3568/tests/moduletest/huks/ -type f | tar -czf ../huks_test.tgz --files-from=-' &&
rm -f ../huks_xts.tgz &&
/bin/bash -c 'find out/rk3568/suites/acts/acts/testcases/ -type f | grep -i huks | sort | tar -czf ../huks_xts.tgz --files-from=-'
'@

$exitCode3 = & C:\Windows\System32\OpenSSH\ssh.exe -i C:\Users\fengziteng\.ssh\id_rsa fengziteng@ubuntu $sshCommand3

# 检查编译结果
if ($LASTEXITCODE -eq 0) {
    Write-Host "编译成功" -ForegroundColor Green
} else {
    Write-Host "编译失败，直接退出" -ForegroundColor Red
    exit $LASTEXITCODE
}

# 第四个 SSH 命令：生成编译数据库
Write-Host "正在生成编译数据库..." -ForegroundColor Green
$sshCommand4 = @'
set -evx ;
cd openharmony/oh-xts-automation/ ;
prebuilts/build-tools/linux-x86/bin/ninja -C out/rk3568 -w dupbuild=warn -t compdb > out/rk3568/cc.json
'@

& C:\Windows\System32\OpenSSH\ssh.exe -i C:\Users\fengziteng\.ssh\id_rsa fengziteng@ubuntu $sshCommand4

# 清理和文件传输
Write-Host "正在清理和传输文件..." -ForegroundColor Green

# 删除本地文件
Remove-Item -Force -Recurse -ErrorAction SilentlyContinue huks_test.tgz
Remove-Item -Force -Recurse -ErrorAction SilentlyContinue huks_xts.tgz

# 从远程复制文件
& C:\Windows\System32\OpenSSH\scp.exe -i C:\Users\fengziteng\.ssh\id_rsa fengziteng@ubuntu:~/openharmony/huks_test.tgz .
& C:\Windows\System32\OpenSSH\scp.exe -i C:\Users\fengziteng\.ssh\id_rsa fengziteng@ubuntu:~/openharmony/huks_xts.tgz .

# 清理输出目录
Remove-Item -Force -Recurse -ErrorAction SilentlyContinue out

# 解压文件
& C:\Windows\System32\tar.exe -xf huks_test.tgz
& C:\Windows\System32\tar.exe -xf huks_xts.tgz

Write-Host "所有操作完成!" -ForegroundColor Green
Read-Host "按回车键继续..."